package com.project.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("经过loginFilter");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //从session中取出登陆信息
        Object userObj =  request.getSession().getAttribute("loginUser");
        if(userObj != null){
            //允许请求继续访问目标资源
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else{
            response.sendRedirect("/html/login.html");
        }
    }
}
